package com.lzx.adx.report.util.biz.product;

import cn.hutool.core.util.URLUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lzx.adx.report.dto.AccountDTO;
import com.lzx.adx.report.dto.ReportDTO;
import com.lzx.adx.report.model.wifi.WifiRequest;
import org.apache.commons.lang3.StringUtils;

import java.nio.charset.StandardCharsets;
import java.util.LinkedHashMap;
import java.util.Map;

public class YiduiChannelHelper {


    public static String covertUrlReport(AccountDTO accountDTO, ReportDTO reportDTO) {
        String callBackUrl=String.format(accountDTO.getDspCallbackUrl(),reportDTO.getBizRequestId());

        JSONObject jsonObject=JSONObject.parseObject(accountDTO.getDspExtend());
        Map<String,Object> params=new LinkedHashMap<>();
        params.put("callback",callBackUrl);
        params.put("os","0");
        params.put("ip",reportDTO.getIp());
        params.put("ua",reportDTO.getUser_agent());
        params.put("click_id",reportDTO.getBizRequestId());
        params.put("cid","");

        if(StringUtils.isNotBlank(reportDTO.getHash_android_id()) && !reportDTO.getHash_android_id().contains("__")) {
            params.put("androidid_md5", reportDTO.getHash_android_id());
        }

        if (StringUtils.isNotBlank(reportDTO.getOaid()) && !reportDTO.getOaid().contains("__")) {
            params.put("oaid", reportDTO.getOaid());
        }
        params.put("ts_ms",System.currentTimeMillis());
        params.put("aid",jsonObject.getString("channelId"));
        String paramsUrl = URLUtil.buildQuery(params, StandardCharsets.UTF_8);
        return accountDTO.getDspReportUrl()+"?"+paramsUrl;
    }
}
